import {Injectable} from '@angular/core';
import {HttpEvent, HttpEventType, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {catchError, Observable, tap} from 'rxjs';

@Injectable()
export class TestRequestInterceptor implements HttpInterceptor {

  constructor() {}

  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    // // 在这里打印请求信息
    console.log('发出查询 Outgoing Request===: ', request);

    // @ts-ignore
    return next.handle(request).pipe(
      tap( (event: HttpEvent<any>) => {
        if(event.type===HttpEventType.Response){
          console.log('收到响应 Incoming Response===: ', event);
        }
      }),
      catchError((error) => {
        console.log('TestRequest 收到错误 Incoming  err :', error);
        return (error);
      })
    );
  }
}
